<?php
/**
 * Created by PhpStorm.
 * User: william
 * Date: 16-8-31
 * Time: 下午2:44
 */
namespace V1;
class E_ProductList extends \ResourceClass {
    public $isActive = true;
    public $remark = '获取产品列表';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require user id ';
        }
        if (!isset($req['userType'])) {
            return 'require user type';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }
        if (!isset($req['enterpriseId'])) {
            return 'require enterprise id';
        }
        if (\Biz\UserHandler::checkAuth($req['userType'],$req['uid'],$req['enterpriseId'],$req['t'],$req['k']) == false) {
            return 'data illegal';
        }
        $l = isset($req['l']) ? $req['l'] : 100000;
        $o = isset($req['o']) ? $req['o'] : 0;
        $name ='';
        $brand_id ='';
        if(isset($req['name'])) {
            $name = $req['name'];
        }
        if (isset($req['brandId'])) {
            $brand_id = $req['brandId'];
        }
        // 实例化列表类
        $tabulation = new \Cls\Tabulation();
        // 初始化列表类相关参数
        $tabulation->setDiyWhere('product_name', $name)
            ->setDiyWhere('brand_id', $brand_id)
            ->setOffset($o)
            ->setLimit($l);
        $product_handler = new \Biz\ProductHandler();
        $product_handler::getProductArrayByEnterpriseId($req['enterpriseId'],$tabulation);
        $total = $tabulation->getTotal();
        if($tabulation->getSuccessStatus()){
            $product_list = array();
            /**
             * @var \Cls\Product\Product $product_cls
             */
            foreach($tabulation->getData() as $product_cls) {
                $tmp_product = array();
                $tmp_product['id'] = $product_cls->getId();
                $tmp_product['enterprise_id'] = $product_cls->getEnterpriseId();
                $tmp_product['brand_id'] = $product_cls->getBrandId();
                $tmp_product['name'] = $product_cls->getName();
                $tmp_product['icon_url'] = $product_cls->dealIconUrl();
                $tmp_product['repertory_warning'] = $product_cls->getStockWarning();
                $tmp_product['individual_packing'] = $product_cls->getSellUnitString(); // 老版本
                $tmp_product['mass_packing'] = $product_cls->getSellUnitString(); // 老版本
                $tmp_product['sell_unit_string'] = $product_cls->getSellUnitString();
                $tmp_product['remark'] = $product_cls->getDescription();
                $tmp_product['insert_time'] = $product_cls->getCreateTime();
                $tmp_product['brandname'] = $product_cls->getBrandInfo()->getName();
                $tmp_product['is_del'] = $product_cls->getDelStatus();
                $tmp_product['insert_time'] = $product_cls->getCreateTime();
                $tmp_product['collect_stock'] = 0;
                foreach ($product_cls->getSkuList() as $sku) {
                    $tmp_product['collect_stock'] += $sku->getStock(); //累加库存信息
                    $tmp_product['stock_warning'] = ($tmp_product['collect_stock'] >= $product_cls->getStockWarning()) ? 0 : 1 ; //是否库存预警
                }
                $product_list[] = $tmp_product;
            }
            try {
                $result = array(
                    'lists'=>$product_list,
                    'total'=>$total,
                );
            } catch (Exception $e) {
                return $e->getMessage();
            }
        }
    }
}